#!/usr/bin  python
# _*_ coding:utf-8 _*_
"""
----------------------------------
    File Name: tags
    Description:
        Author: lhw
        Date: 2020/8/7
-----------------------------------
"""
from rest_framework.views import APIView
from rest_framework.response import Response
from extensions.auth import JwtAuthorizationAuthentication
from Admin import models
from Admin.serializers import tag
from Common.logger import app_log
from Common.response import resp


class TagView(APIView):
    authentication_classes = [JwtAuthorizationAuthentication, ]

    def get(self, request, *args, **kwargs):
        try:
            tag_query = models.Tag.objects.all()
            tag_data = tag.TagModelSerializer(tag_query, many=True).data
            return Response({
                'error_code': 0,
                'message': "Success",
                'data': tag_data
            })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)

    def post(self, request, *args, **kwargs):
        try:
            request_data = request.data
            tag_title = request_data["tag_title"]
            data = models.Tag.objects.filter(tag_title=tag_title)
            if data.exists():
                return resp(1006, {})
            tag_ser = tag.TagModelSerializer(data=request_data)
            tag_ser.is_valid(raise_exception=True)
            tag_obj = tag_ser.save()
            return Response({
                'error_code': 0,
                'message': "Success",
                'data': tag.TagModelSerializer(tag_obj).data
            })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)

    def put(self, request, *args, **kwargs):
        try:
            request_data = request.data
            tag_title = request_data["tag_title"]
            tag_id = request_data["id"]
            data = models.Tag.objects.filter(tag_title=tag_title).exclude(id=tag_id)
            if data.exists():
                return resp(1006, {})
            else:
                models.Tag.objects.filter(id=tag_id).update(tag_title=tag_title)
                return Response({
                    'error_code': 0,
                    'message': "Success"
                })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)

    def delete(self, request, *args, **kwargs):
        try:
            tag_id = request.GET.get("id")
            models.Tag.objects.filter(id=tag_id).delete()
            return Response({
                'error_code': 0,
                'message': "Success",
            })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)


class SearchTag(APIView):
    authentication_classes = [JwtAuthorizationAuthentication, ]

    def get(self, request, *args, **kwargs):
        try:
            params = request.query_params
            search = params["search"]
            tag_query = models.Tag.objects.filter(tag_title__icontains=search).all()
            if tag_query.exists():
                tag_data = tag.TagModelSerializer(tag_query, many=True).data
                return Response({
                    'error_code': 0,
                    'message': "Success",
                    'data': tag_data
                })
            else:
                return Response({
                    'error_code': 0,
                    'message': "Success",
                    'data': []
                })
        except Exception as e:
            app_log.exception(e)
            return resp(-1, e.args)
